﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataParser;

namespace argusThreeAndAHalfMen
{
    public partial class frmEditProduct : Form
    {
        //pointers
        private Product product;
        private frmBranchDetails branchDetails;
        private Category selectedCategory;
        private Branch branch;

        //FORM EDIT PRODUCT
        public frmEditProduct(Product product, frmBranchDetails branchDetails, Category selectedCategory, Branch branch)
        {//set relevant pointers
            InitializeComponent();
            this.product = product;
            this.branchDetails = branchDetails;
            this.selectedCategory = selectedCategory;
            this.branch = branch;
        }

        //BTN SVE CLICK EVENT
        private void btnSave_Click(object sender, EventArgs e)
        {//on save, sets all relevant data from form objects using pointers
            string messageBoxText = "Are you sure you want to make these changes to the product?";
            string caption = "Edit product";
            MessageBoxButtons button = MessageBoxButtons.YesNo;
            DialogResult result;

            //before yesno box, attempts conversion of units to ensure that number boxes are filled correctly to prevent crashing
            try
            {
                Convert.ToSingle(txtAddProductPrice.Text);
            }
            catch
            {
                MessageBox.Show("Please enter a valid price.", "Invalid price");
                return;
            }

            try
            {
                Convert.ToInt32(txtAddProductStock.Text);
            }
            catch
            {
                MessageBox.Show("Please enter a valid stock level.", "Invalid stock level");
                return;
            }


            // Display message box
            result = MessageBox.Show(messageBoxText, caption, button);

            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                product.ProductDesc = txtAddProductDetails.Text;
                product.ProductName = txtAddProductName.Text;
                product.ProductPrice = Convert.ToSingle(txtAddProductPrice.Text);
                product.ProductStockLevel = Convert.ToInt32(txtAddProductStock.Text);
                product.ProductImagePath = imgProductImage.ImageLocation;

                bool productFound = false;
                bool categoryFound = false;
                foreach (Category category in branch.Categories)
                {
                    if (cboExistingCategories.Text.Substring(0, cboExistingCategories.Text.Length - 6) == category.CategoryName)
                        categoryFound = true;

                    foreach (Product thisproduct in category.ProductList)
                        if (thisproduct.ProductID == product.ProductID)
                        {
                            productFound = true;
                            selectedCategory = category;
                        }
                }
                if (productFound == true)
                {
                    selectedCategory.ProductList.Remove(product);
                    if (selectedCategory.ProductList.Count == 0)
                        branch.Categories.Remove(selectedCategory);
                }

                if (categoryFound != true)
                {
                    Category newCategory = new Category();
                    newCategory.CategoryID = Convert.ToInt32(cboExistingCategories.Text.Substring(cboExistingCategories.Text.IndexOf("(") + 1, 3));
                    newCategory.CategoryName = cboExistingCategories.Text.Substring(0, cboExistingCategories.Text.Length - 6);
                    newCategory.ProductList.Add(product);

                    branch.Categories.Add(newCategory);
                }

                else if (categoryFound == true)
                {
                    foreach (Category category in branch.Categories)
                        if (cboExistingCategories.Text.Substring(0, cboExistingCategories.Text.Length - 6) == category.CategoryName)
                            category.ProductList.Add(product);
                }
            }
            else if (result == System.Windows.Forms.DialogResult.No)
                return;

            Parser.SaveBranchList("argus-BIG.txt"); //call save method

            //after saving, refreshes branch form
            branchDetails.refreshProductDetails();
            branchDetails.refreshCategories();
            branchDetails.resetListboxSelection();            

            this.Close();
        }
        
        //BTN CANCEL CLICK EVENT
        private void btnCancel_Click(object sender, EventArgs e)
        {//closes form
            this.Close();
        }

        //FORM EDIT PRODUCT LOAD EVENT
        private void frmEditProduct_Load(object sender, EventArgs e)
        {//loads relevant data into form objects using pointers
            txtAddProductDetails.Text = product.ProductDesc;
            txtAddProductName.Text = product.ProductName;
            txtAddProductPrice.Text = product.ProductPrice.ToString();
            txtAddProductStock.Text = product.ProductStockLevel.ToString();
            lblProductID.Text = product.ProductID.ToString();
            imgProductImage.ImageLocation = product.ProductImagePath;
            
            cboExistingCategories.Items.Add("Clothing (999)");
            cboExistingCategories.Items.Add("Garden & DIY (998)");
            cboExistingCategories.Items.Add("Health & Personal Care (997)");
            cboExistingCategories.Items.Add("Home Entertainment (996)");
            cboExistingCategories.Items.Add("Home & Furniture (995)");
            cboExistingCategories.Items.Add("Jewellery & Watches (994)");
            cboExistingCategories.Items.Add("Kitchen & Laundry (993)");
            cboExistingCategories.Items.Add("Nursery (992)");
            cboExistingCategories.Items.Add("Toys & Games (991)");
            cboExistingCategories.Items.Add("Sports & Leisure (990)");

            cboExistingCategories.Text = selectedCategory.CategoryName + " (" + selectedCategory.CategoryID + ")";
        }

        //BTN BROWSE FILE CLICK EVENT
        private void btnBrowseFile_Click(object sender, EventArgs e)
        {//opens file browser to allow user to choose a picture
            OpenFileDialog browseForImage = new OpenFileDialog();
            browseForImage.Title = "Choose a product image";
            browseForImage.InitialDirectory = @"c:\";
            browseForImage.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            browseForImage.FilterIndex = 2;
            browseForImage.RestoreDirectory = true;
            if (browseForImage.ShowDialog() == DialogResult.OK)
            {
                imgProductImage.ImageLocation = browseForImage.FileName;
            }
        }
    }
}
